package de.ars.client.items;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;


import com.sun.lwuit.Font;

import de.ars.client.Main;
import de.ars.client.animator.Animator;
import de.ars.client.animator.IAnimationEventListener;
import de.ars.client.animator.Point;
import de.ars.client.datahandling.PropertyReader;
import de.ars.client.items.animators.SendSMSAnimator;
import de.ars.client.items.elements.AbstractItem;
import de.ars.client.items.elements.ListItem;
import de.ars.client.items.elements.SmsItem;
import de.ars.client.paintSystems.ClassicLayout;
import de.ars.client.paintSystems.StringPainter;
import de.ars.client.screens.MainScreen;
import de.ars.common.IDataModelConstants;

public class SendSMS extends AbstractSlideItem
{
	
	private String message;
	
    public SendSMS(int _width, int _height, MainScreen _parent, StringPainter _stringPainter)
    {
    	super(_width,_height,_parent,_stringPainter);
    	this.animator = new SendSMSAnimator(this);
        this.animator.createNewAnimationPoint(this.layout.getAddPersonClosePos());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonCloseSize());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonCloseTextPos());
        this.animator.setLayout(this.layout);

        entryItems = new Vector();
        SmsItem tempItem = new SmsItem(this.stringPainter, "");
        tempItem.setHeight(100);
        tempItem.setWidth(200);
        entryItems.addElement(tempItem);
    }
    
    public void setIndex(int _index)
    {
    	this.selectedItemIndex = _index;
    }
        
    public boolean keyPressed(int _key)
	{
    	if (isOpen())
    	{
	    	switch(_key)
	    	{
	    		case -7:
	    			//SMS senden
	    			close();
	    			this.parent.sendSMSToSMSSender(((AbstractItem)this.entryItems.elementAt(0)).getValue());
	    			this.parent.openSMSSender();
	    			return true;
	    		case -6:
	    			//Abbrechen
	    			close();
	    			this.parent.clearSMSText();
	    			return true;
	    		default:
	    			if (
	    			((SmsItem) this.entryItems.elementAt(0)).getEditableText().length() <= 159 
	    			|| (((SmsItem) this.entryItems.elementAt(0)).getCharOption() 
	    			&& ((SmsItem) this.entryItems.elementAt(0)).getEditableText().length()==159)
	    			|| this.isDeleteKey(_key)
	    			)
	    				((AbstractItem)this.entryItems.elementAt(0)).keyPressed(_key);
	    			return true;
	    	}
    	}
    	return false;
	}
    
    private boolean isDeleteKey(int key)
	{
		for (int i = 0 ; i < IDataModelConstants.KEYS_DELETE.length;i++)
		{
			if (IDataModelConstants.KEYS_DELETE[i] == key)
				return true;
		}
		return false;
	}
    
    public void clearSMSText()
    {
    	((AbstractItem)this.entryItems.elementAt(0)).setValue("");
    }
    
    public int getCharCountToWrite()
    {
    	return 160 - ((SmsItem) this.entryItems.elementAt(0)).getEditableText().length();
    }
    
    public void paint(Graphics g)
    {
    	super.paint(g);
    	this.stringPainter.drawString(g, "SMS", 5, 0);
    	int chars = getCharCountToWrite();
    	int startPos = ((SmsItem) this.entryItems.elementAt(0)).getSmsWidth()-((SmsItem) this.entryItems.elementAt(0)).getFont().stringWidth(""+chars+"");
    	this.stringPainter.drawString(g, "SMS", 5, 0);
    	this.stringPainter.drawString(g, ""+chars+"", startPos, 0);
    }
}
